<?php
class RCMSAdminCtrl extends RCMSControllerBase {

	protected  function setComponents() {
		$admincomponents = array(
		array('url'=>'siteconfig', 'name'=>'Site Setting', 'img'=>'setup.png'),
		array('url'=>'siteroute', 'name'=>'Site Route', 'img'=>'setup.png'),
		array('url'=>'sitemenu', 'name'=>'Site Menu', 'img'=>'menu.png'),
		array('url'=>'addpage', 'name'=>'New Page', 'img'=>'addpage.png'),
		array('url'=>'addpage', 'name'=>'New Page', 'img'=>'addpage.png'),
		array('url'=>'addpage', 'name'=>'New Page', 'img'=>'addpage.png'),
		array('url'=>'addpage', 'name'=>'New Page', 'img'=>'addpage.png'),
		);
		$this->setContent('admincomponents', $admincomponents);
	}

	public function indexAction(){
	}

	public function homeAction(){
		$this->setContent('site_template','admin/admin_index.html');
		$this->setContent('admin_action','home');
		$this->setComponents();
	}
	public function siteconfigAction(){
		$this->setContent('site_template','admin/admin_index.html');
		$this->setContent('admin_action','siteconfig');		
		$request = $this->getApp()->getRequest();
		$results = $this->querySiteParameters('00','00','*');
		if ($request->isPost()) {
			$tableName = 'RCMS_SITE_PARAM';
			$updateColumns = array(
				'PARAM_TEXT = :PARAM_TEXT',
				'R_DEL_FLG = :R_DEL_FLG',
				'R_UPD_DATE = :R_UPD_DATE',
				'R_UPD_USR = :R_UPD_USR',
				'R_UPD_PGM = :R_UPD_PGM',
			);
			$updateWhereClause = array(
				'SYSTEM_ID = :SYSTEM_ID',
				'SUBSYS_ID = :SUBSYS_ID',
				'SYSTEM_LANG = :SYSTEM_LANG',
				'PARAM_TYPE = \'00\'',
				'PARAM_ID = :PARAM_ID',
			);
			$dao = $this->getApp()->getDbManager()->getDAO('RcmsTable');
			try {
				$dao->beginTransaction();
				foreach ($results as $result) {
					$params = array(
						'PARAM_ID' => $result['PARAM_ID'],
						'PARAM_TEXT' =>$request->getPost($result['PARAM_ID']),
						'R_DEL_FLG' => ($request->getPost($result['PARAM_ID']).'_delFlag' == '1' ? 1 : 0),
						'R_UPD_DATE' => date("Y/m/d H:m:s"),
						'R_UPD_USR' => 'webadmin',
						'R_UPD_PGM' => get_class($this),
						'SYSTEM_ID' => '00',
						'SUBSYS_ID' => '00',
						'SYSTEM_LANG' => '*',
					);
					$dao->updateDataByTableName($tableName, $updateColumns, $updateWhereClause, $params);
				}
				$dao->commit();
				$results = $this->querySiteParameters('00','00','*');
			} catch (Exception $e) {
				$dao->rollBack();
				RCMSLog::trace($e);
			}
		}		
		$datas = array();
		foreach ($results as $result) {
			$datas[] = array('name'=>$result['PARAM_ID'], 'value'=>$result['PARAM_TEXT']);
		}
		$this->setContent('rcms_datas_caption','siteconfig');
		$this->setContent('rcms_datas',$datas);
	}

	public function siterouteAction(){
		$this->setContent('site_template','admin/admin_index.html');
		$this->setContent('admin_action','siteroute');		
		$results = $this->getApp()->getRoutes();
		$request = $this->getApp()->getRequest();
		if ($request->isPost()) {
			/*
			$tableName = 'RCMS_ROUTE';
			$updateColumns = array(
				'PARAM_TEXT = :PARAM_TEXT',
				'R_DEL_FLG = :R_DEL_FLG',
				'R_UPD_DATE = :R_UPD_DATE',
				'R_UPD_USR = :R_UPD_USR',
				'R_UPD_PGM = :R_UPD_PGM',
			);
			$updateWhereClause = array(
				'SYSTEM_ID = :SYSTEM_ID',
				'SUBSYS_ID = :SUBSYS_ID',
				'SYSTEM_LANG = :SYSTEM_LANG',
				'PARAM_TYPE = \'00\'',
				'PARAM_ID = :PARAM_ID',
			);
			$dao = $this->getApp()->getDbManager()->getDAO('RcmsTable');
			try {
				$dao->beginTransaction();
				foreach ($results as $result) {
					$params = array(
						'PARAM_ID' => $result['PARAM_ID'],
						'PARAM_TEXT' =>$request->getPost($result['PARAM_ID']),
						'R_DEL_FLG' => ($request->getPost($result['PARAM_ID']).'_delFlag' == '1' ? 1 : 0),
						'R_UPD_DATE' => date("Y/m/d H:m:s"),
						'R_UPD_USR' => 'webadmin',
						'R_UPD_PGM' => get_class($this),
						'SYSTEM_ID' => '00',
						'SUBSYS_ID' => '00',
						'SYSTEM_LANG' => '*',
					);
					$dao->updateDataByTableName($tableName, $updateColumns, $updateWhereClause, $params);
				}
				$dao->commit();
				$results = $this->querySiteParameters('00','00','*');
			} catch (Exception $e) {
				$dao->rollBack();
				RCMSLog::trace($e);
			}
			*/
		}
		$columns = array(
			'ROUTE_ID',
			'REQ_TYPE',
			'REQ_URL',
			'REQ_CTRL',
			'REQ_ACTION',
			'REQ_PARAMS',
		);
		$datas = array();
		foreach ($results as $result) {
			$defs = $result->getDefinitions();
			$data = array();
			foreach ($columns as $col) {
				$data[$col] = (is_null($defs[$col]) ? '' : $defs[$col]);
			}
			$datas[] = $data;
		}
		$this->setContent('rcms_datas_caption','routeconfig');
		$this->setContent('rcms_columns',$columns);
		$this->setContent('rcms_datas',$datas);
	}
	
	public function sitemenuAction(){
		$this->setContent('site_template','admin/admin_index.html');
		$this->setContent('admin_action','sitemenu');
		$results = $this->getApp()->getMenus();
		$request = $this->getApp()->getRequest();
		if ($request->isPost()) {
		}
		
		$columns = array(
			'MENU_ID',
			'MENU_PARENT_ID',
			'MENU_TYPE',
			'MENU_GROUP_NAME',
			'MENU_NAME',
			'MENU_TITLE',
			'MENU_DISP_FLG',
			'DISP_ORDER',
		);
		$datas = array();
		foreach ($results as $result) {
			$defs = $result->getDefinitions();
			$data = array();
			foreach ($columns as $col) {
				$data[$col] = (is_null($defs[$col]) ? '' : $defs[$col]);
			}
			$datas[] = $data;
		}
		$this->setContent('rcms_datas_caption','menuconfig');
		$this->setContent('rcms_columns',$columns);
		$this->setContent('rcms_datas',$datas);
	}
	
	protected function compileTable($className, $tableName, $selectColumns, $selectWhere, $selectOrder, $selectParams, $updateColumns, $updateWhereClause, $updateParamNames) {
		$dao = $this->getApp()->getDbManager()->getDAO('RcmsTable');
		$records = $dao->fetchAllDataByTableName($tableName, $selectColumns, $selectWhere, $selectOrder, $selectParams);
		$objs = array();
		foreach ($records as $record) {
			$obj = new $className($record);
			$objs[] = $obj;
		}
		foreach ($objs as $obj) {
			$obj->compile($objs);
			$defs = $obj->getDefinitions();
			$params = array();
			foreach ($updateParamNames as $paramNames) {
				$params[$paramNames] = $defs[$paramNames];
			}
			$dao->updateDataByTableName($tableName, $updateColumns, $updateWhereClause, $params);
		}
	}

	public function recompileAllRoute() {
		$tableName = 'RCMS_ROUTE';
		$className = 'RCMSRouter';
		$selectColumns = array(
			'SYSTEM_ID',
			'SUBSYS_ID',
			'SYSTEM_LANG',
			'ROUTE_ID',
			'REQ_TYPE',
			'REQ_URL',
			'REQ_CTRL',
			'REQ_ACTION',
			'REQ_EXTS',
			'REQ_PARAMS',
			'PARSED_URL',
			'PARSED_MAP',
		);
		$selectWhere = null;
		$selectOrder = null;
		$selectParams = null;
		$updateColumns = array('PARSED_URL=:PARSED_URL','PARSED_MAP=:PARSED_MAP');
		$updateWhereClause = array('SYSTEM_ID=:SYSTEM_ID', 'SUBSYS_ID=:SUBSYS_ID','SYSTEM_LANG=:SYSTEM_LANG','ROUTE_ID=:ROUTE_ID');
		$updateParamNames = array('SYSTEM_ID', 'SUBSYS_ID', 'SYSTEM_LANG', 'ROUTE_ID', 'PARSED_URL', 'PARSED_MAP');
		$this->compileTable($className, $tableName, $selectColumns, $selectWhere, $selectOrder, $selectParams, $updateColumns, $updateWhereClause, $updateParamNames);
	}

	public function recompileAllMenu() {
		$tableName = 'RCMS_MENU';
		$className = 'RCMSMenu';
		$selectColumns = array(
			'SYSTEM_ID',
			'SUBSYS_ID',
			'SYSTEM_LANG',
			'MENU_ID',
			'MENU_PARENT_ID',
			'MENU_TYPE',
			'MENU_GROUP_NAME',
			'MENU_NAME',
			'MENU_TITLE',
			'MENU_DISP_FLG',
			'DISP_ORDER',
			'PARSED_DEEP',
			'PARSED_PATH',
		);
		$selectWhere = null;
		$selectOrder = array('SYSTEM_ID ASC', 'SUBSYS_ID ASC','SYSTEM_LANG ASC','MENU_PARENT_ID ASC','DISP_ORDER ASC');
		$selectParams = null;
		$updateColumns = array('PARSED_DEEP=:PARSED_DEEP','PARSED_PATH=:PARSED_PATH');
		$updateWhereClause = array('SYSTEM_ID=:SYSTEM_ID', 'SUBSYS_ID=:SUBSYS_ID','SYSTEM_LANG=:SYSTEM_LANG','MENU_ID=:MENU_ID');
		$updateParamNames = array('SYSTEM_ID', 'SUBSYS_ID', 'SYSTEM_LANG', 'MENU_ID', 'PARSED_DEEP', 'PARSED_PATH');
		$this->compileTable($className, $tableName, $selectColumns, $selectWhere, $selectOrder, $selectParams, $updateColumns, $updateWhereClause, $updateParamNames);
	}
}
?>
